package com.dz.lm.service.comp.compproduct.impl;

import com.dz.lm.common.dto.AttachDTO;
import com.dz.lm.common.dto.CompProudctDTO;
import com.dz.lm.common.dto.CompUserDTO;
import com.dz.lm.common.dto.PageDto;
import com.dz.lm.common.exception.DaoException;
import com.dz.lm.common.exception.ServiceException;
import com.dz.lm.common.util.StringUtil;
import com.dz.lm.dao.compproduct.read.CompProductDao;
import com.dz.lm.dao.userproduct.write.UserProductDao;
import com.dz.lm.domain.compproduct.CompProduct;
import com.dz.lm.domain.compproduct.CompProductExample;
import com.dz.lm.domain.userproduct.UserProduct;
import com.dz.lm.domain.userproduct.UserProductExample;
import com.dz.lm.service.attach.AttachService;
import com.dz.lm.service.comp.compproduct.CompProductService;
import com.dz.lm.service.comp.user.CompUserService;
import com.dz.lm.service.userapply.UserApplyService;
import com.dz.lm.service.userproduct.UserProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CompProductServiceImpl implements CompProductService {
    @Resource(name = "readCompProductDao")
    private CompProductDao readCompProductDao;
    @Resource(name = "writeCompProductDao")
    private com.dz.lm.dao.compproduct.write.CompProductDao writeCompProductDao;
    @Autowired
    private CompUserService compUserService;
    @Autowired
    private UserProductService userProductService;
    @Resource(name = "writeUserProductDao")
    private UserProductDao writeUserProductDao;
    @Resource(name = "readUserApplyDao")
    private com.dz.lm.dao.userapply.read.UserApplyDao readUserApplyDao;
    @Resource(name = "readUserProductDao")
    private com.dz.lm.dao.userproduct.read.UserProductDao readUserProductDao;
    @Autowired
    private AttachService attachService;
   @Autowired
   private UserApplyService userApplyService;

    @Override
    public int add(CompProduct product) throws ServiceException {
        int ret =0;
        ret = writeCompProductDao.insertSelective(product);
        if(ret > 0){
            PageDto query = compUserService.query(1, 9999, product.getCompId(), 0, 1);
            if(query.getList() != null && query.getList().size() > 0){
                List<CompUserDTO> users = query.getList();
                for (CompUserDTO user : users) {
                    UserProduct userProduct = new UserProduct();
                    userProduct.setCompId(product.getCompId());
                    BeanUtils.copyProperties(product,userProduct);
                    userProduct.setProductType(1);
                    userProduct.setSuperId(0);
                    userProduct.setStatus(1);
                    userProduct.setSource(user.getUserId());
                    userProduct.setUserId(user.getUserId());
                    userProduct.setCompPid(product.getProductId());
                    userProductService.add(userProduct);
                }
            }
        }
        return ret;
    }

    @Override
    public int update(CompProduct product) throws ServiceException {
        int ret =0;
        ret = writeCompProductDao.updateByPrimaryKeySelective(product);
        try {
            writeUserProductDao.updateByCompProduct(product);
        } catch (DaoException e) {
            e.printStackTrace();
        }
        return ret;
    }

    @Override
    public int delete(int productId) throws ServiceException {
        return 0;
    }

    @Override
    public PageDto query(int pageNum,int rows,int compId) throws ServiceException {
        PageDto pageDto = new PageDto();
        PageHelper.startPage(pageNum,rows);
        List<Map> list = readCompProductDao.query(compId);
        if(list != null && list.size() > 0){
            PageInfo pageInfo = new PageInfo(list);
            for (Map map : list) {
                try {
                    int applyCount = readUserApplyDao.queryCompProudctCount(StringUtil.getInt(map.get("product_id"), 0));
                    UserProductExample ex = new UserProductExample();
                    UserProductExample.Criteria cr = ex.createCriteria();
                    ex.setCompPid(StringUtil.getInt(map.get("product_id"), 0));
                    cr.andStatusEqualTo(2);//发布
                    cr.andProductTypeEqualTo(1);//
                    int lunCount = readUserProductDao.querCount(ex);//发布数量
                    UserProductExample ex1 = new UserProductExample();
                    UserProductExample.Criteria cr1 = ex1.createCriteria();
                    ex1.setCompPid(StringUtil.getInt(map.get("product_id"), 0));
                    List<Integer> satList = new ArrayList();
                    satList.add(1);
                    satList.add(2);
                    cr1.andStatusIn(satList);//发布
                    cr1.andProductTypeEqualTo(2);//
                    int agentCount = readUserProductDao.querCount(ex1);//代理数量
                    map.put("agentCount",agentCount);
                    map.put("lunCount",lunCount);
                    map.put("applyCount",applyCount);
                    map.put("createTime",StringUtil.getString(map.get("createTime")).substring(0,16));
                } catch (DaoException e) {
                    e.printStackTrace();
                }
            }
            pageDto.setInfo(pageInfo);
            pageDto.setList(list);
        }
        return pageDto;
    }

    @Override
    public CompProudctDTO get(int productId) throws ServiceException {
        CompProudctDTO dto = null ;
        CompProduct product = readCompProductDao.selectByPrimaryKey(productId);
        if(product != null){
            dto = new CompProudctDTO();
            BeanUtils.copyProperties(product,dto);
            //添加视频信息
            if (dto.getVideoId() > 0) {
                AttachDTO attachDTO = attachService.sleletById(dto.getVideoId());
                if (attachDTO != null) {
                    dto.setVideoInfo(attachDTO);
                }
            }
        }
        return dto;
    }
}
